package com.hsq.ndmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsq.ndmall.product.entity.CategoryEntity;
import com.hsq.ndmall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hsq.ndmall.product.dao.GoodsDao;
import com.hsq.ndmall.product.entity.GoodsEntity;
import com.hsq.ndmall.product.service.GoodsService;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.Query;

@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    GoodsDao goodsDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GoodsEntity> page = this.page(new Query<GoodsEntity>().getPage(params), generatorWrapper(params));

        return new PageUtils(page);
    }

    @Override
    public List<GoodsEntity> queryByCategory(List<Integer> l2List, int offset, int limit) {
        System.out.println("==============================================================================");
        return goodsDao.selectPage(new Page(offset, limit), Wrappers.<GoodsEntity>lambdaQuery().
                in(GoodsEntity::getCategoryId, l2List).
                eq(GoodsEntity::getIsOnSale, true).
                eq(GoodsEntity::getDeleted, false).
                orderByDesc(GoodsEntity::getAddTime)).getRecords();
    }


    private QueryWrapper generatorWrapper(Map<String, Object> params) {
        System.out.println("==================");
        QueryWrapper<GoodsEntity> goodsEntityQueryWrapper = new QueryWrapper<>();
        // 判断是否传入参数是否包含key为isNew，如果包含就条件查询GoodsEntity的值为map中的key为isNEw的值
        // 判断商品是为新品首发
        if (params.containsKey("isNew")) {
            goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getIsNew, params.containsKey("isNew"));
        }
        //  判断商品是否为首页推荐
        if (params.containsKey("isHot")) {
            goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getIsHot, params.containsKey("isHot"));
        }
        if (params.containsKey("goods_id")) {
            goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getId, params.containsKey("goods_id"));
        }
        //  判断是否传入了商品Id，如果传入根据ID查询商品推荐
        if (params.containsKey("goodsId")) {
            GoodsEntity goodsEntity = goodsDao.selectById(Integer.parseInt(((String) params.get("goodsId"))));
            // 返回商品信息
            goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getCategoryId,goodsEntity.getCategoryId());
        }
        if(params.containsKey("category_id")){
            goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getCategoryId,params.get("category_id"));
        }
        // 查询的商品必须时未删除的
        goodsEntityQueryWrapper.lambda().eq(GoodsEntity::getDeleted, 0);
        return goodsEntityQueryWrapper;
    }

}
